

/**
 * 画多边形圆
 */
import Draw from "./Draw";
import PolygonRound from '../../entity/type/PolygonRound'
import * as turf from '@turf/turf'
import Transform from '../../transform/Transform'
import TurfTransform from '../../transform/TurfTransform'

const DEF_STYLE = {
  material: Cesium.Color.YELLOWGREEN.withAlpha(0.6)
}

export default class DrawPolygonRound extends Draw {

  constructor(style) {
    super()
    this._style = {
      ...DEF_STYLE,
      ...style
    }
    this._positionsRound = []
    this._positionCoordinate = []
    this._radius = 0
    this._message = '左键点击开始, 再次点击结束'
  }

  _mountEntity() {

    this._delegate = new Cesium.Entity({
      polygon: {
        ...this._style,
        hierarchy: new Cesium.CallbackProperty(() => {
          if(this._positionsRound.length > 1){
            return new Cesium.PolygonHierarchy(this._positionsRound)
          }
        }, false)
      }
    })
    this._layer.add(this._delegate)
  }

  _onClick(e) {
    e = this.getMouseInfo(e.position)
    let position = this._clampToGround ? e.surfacePosition : e.position
    if (!position) {
      return false
    }
    let len = this._positions.length
    this._positions.push(position)
    if (len === 0) {
      this._positions.push(position)
      this.createAnchor(position)
      this._floatingAnchor = this.createAnchor(position)
    }

    if (len > 1) {
      this._positions.pop()
      this.unbindEvent()
      let polygonRound = new PolygonRound(
       this._positionsRound,
       this._positionCoordinate
      )
      polygonRound.setStyle(this._style)
      this._plotEvent.raiseEvent(polygonRound)
    }
  }

  /**
   * 计算半径  根据半径生成多边形 根据起始点和结束点计算角度 进行旋转
   * @param {*} e 
   * @returns 
   */
  _onMouseMove(e) {
    e = this.getMouseInfo(e.endPosition)
    this._tooltip.showAt(e.windowPosition , this._message)
    if (this._floatingAnchor) {
      let position = this._clampToGround ? e.surfacePosition : e.position
      if (!position) {
        return false
      }
      // this._floatingAnchor.position.setValue(position)
      this._radius = Cesium.Cartesian3.distance(
        this._positions[0],
        position
      )
      this._radius *= 1.6

      this.computedPosition(this._radius)
      let angle = this.computedPointAngle(this._positions[0] , position)
      let anglePositionRound = this.computedRotate(angle)
      this._positionsRound = new Cesium.Cartesian3.fromDegreesArray(anglePositionRound)
    }
  }

  /**
   * 根据半径计算多边形位置
   * @param {*} radius 
   */
  computedPosition(radius){
    let center = Transform.Cartesian3ToWGS84(this._viewer, this._positions[0])
    center = [center[0] , center[1]]
    let options = {steps: 5}
    let circle = turf.circle(center, radius / 1609 , options)
    let coordinates = circle.geometry.coordinates[0]
    let positions = TurfTransform.TurfCoordArrayToWGS84CoordArray(coordinates)
    this._positionCoordinate = coordinates
    return positions
  }
  
  /**
   * 计算两点之间角度
   * @param {*} startPoint 
   * @param {*} endPoint 
   */

  computedPointAngle(startPoint , endPoint){
    startPoint = Transform.Cartesian3ToWGS84(this._viewer, startPoint)
    startPoint = [startPoint[0] , startPoint[1]]

    endPoint = Transform.Cartesian3ToWGS84(this._viewer, endPoint)
    endPoint = [endPoint[0] , endPoint[1]]

    let point1 = turf.point(startPoint)
    var point2 = turf.point(endPoint)

    return turf.bearing(point1, point2)
  }
  
  /**
   * 计算旋转角度
   * @param {*} angle 
   */
  computedRotate(angle){
    let poly = turf.polygon( [ this._positionCoordinate ])
    let rotatedPoly = turf.transformRotate(poly, angle)
    let coordinates = rotatedPoly.geometry.coordinates[0]
    let positions = TurfTransform.TurfCoordArrayToWGS84CoordArray(coordinates)

    this._positionCoordinate = coordinates
    return positions
  }
}